<%--
  Created by IntelliJ IDEA.
  User: 86158
  Date: 2022/10/23
  Time: 16:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HRM</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>

<div class="layui-card">
    <!-- 数据表格start -->
    <div class="layui-card-body">
        <table class="layui-hide" id="List" lay-filter="List-toolbar"></table>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">一键审核</button>
                <button class="layui-btn layui-btn-sm layui-bg-blue" id="reloadTest">
                    刷新列表
                </button>
            </div>
        </script>

        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-danger layui-btn-xs deleteBtn" lay-event="audit">审核</a>
        </script>
    </div>
    <!-- 数据表格end -->
</div>
<script>
    layui.use(['table', 'dropdown', 'jquery'], function () {
        var table = layui.table;
        var dropdown = layui.dropdown;
        var $ = layui.jquery;
        var test = ${loginUser.status};
        // console.log();


        // 创建渲染数据表格
        var tableIns = table.render({
            elem: '#List'
            , url: '${pageContext.request.contextPath}/user/auditList?status=0'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: ['filter', 'exports', 'print', {
                title: '帮助'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , height: "full-" + Math.round(Number($('.layui-card-header').height()) + 60)
            , cellMinWidth: 80
            , page: true,
            limits:[5,10,20,50,100],
            cols: [[
                {type: "checkbox", fixed: "left", width: 50},
                {field: 'id', title: 'id', minWidth: 100, align: "center"},
                {field: 'loginName', title: '账号', minWidth: 100, align: "center"},
                {
                    field: 'status', title: '状态', templet: function (d) {
                        if (d.status == "-1") {
                            return "审核不通过";
                        } else if (d.status == "0") {
                            return "待审核";
                        } else if (d.status == "1") {
                            return "普通用户";
                        } else if (d.status == "2") {
                            return "超级管理员";
                        }

                    }, minWidth: 100, align: "center"
                },
                {
                    field: 'createDate', title: '注册时间', templet: function (d) {
                        var toDateString = layui.util.toDateString(d.createDate, "yyyy-MM-dd HH:mm:ss");
                        return toDateString;

                    }, minWidth: 100, align: "center"
                },
                {field: 'userName', title: '用户名', minWidth: 100, align: "center"},
                {title: '操作', width: 160, templet: '#barDemo', fixed: "right", align: "center"}
            ]]
            , done: function (data) {
                var dom = $("#count", window.parent.document);
                dom.text(data.count);
                var id = this.id;

                $('#reloadTest').click(function () {
                    table.reload('List');
                });

                if (test != 2) {
                    $('.deleteBtn').addClass("layui-btn-disabled").prop("disabled", true);
                    $('.editBtn').addClass("layui-btn-disabled").prop("disabled", true);
                }
            }
            , error: function (res, msg) {
                console.log(res, msg)
            }
        });

        // $('#SearchBtn').on('click', function () {
        //     var type = $(this).data('type');
        //     active[type] ? active[type].call(this) : '';
        // });

        // 工具栏事件
        table.on('toolbar(List-toolbar)', function (obj) {
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch (obj.event) {
                case 'LAYTABLE_TIPS':
                    layer.alert('Table for layui-v' + layui.v);
                    break;
                case 'getCheckData':
                    var data = checkStatus.data;
                    var url = "${pageContext.request.contextPath}/user/audit?";
                    data.forEach(function (element) {
                        url += "ids=" + element.id + "&";
                    });
                    var url = url.substring(0,url.length-1);
                    layer.confirm('是否确认审核通过', function (index) {
                        $.ajax({
                            url: url,
                            contentType: "application/json;charset=utf-8",
                            dataType: 'json',
                            success: function (data) {
                                if (data == "1") {
                                    layer.msg("操作成功", {
                                        icon: 1,
                                        success: function () {
                                            $('#reloadTest').trigger('click');
                                        }
                                    });
                                }
                            },
                            error: function (e) {

                            }
                        })
                        layer.close(index);
                    });

                    break;
            }
            ;
        });

        //触发单元格工具事件
        table.on('tool(List-toolbar)', function (obj) { // 双击 toolDouble
            var data = obj.data;
            if (obj.event === 'audit') {
                layer.confirm('是否确认审核通过', function (index) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/user/audit?ids=" + data.id,
                        contentType: "application/json;charset=utf-8",
                        dataType: 'json',
                        success: function (data) {
                            if (data == "1") {
                                layer.msg("操作成功", {
                                    icon: 1,
                                    success: function () {
                                        $('#reloadTest').trigger('click');

                                    }
                                });
                            }
                        },
                        error: function (e) {

                        }
                    })
                    layer.close(index);
                });
            }
        });
    });
</script>

</body>
</html>
